package com.stx.jeepay.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.stx.jeepay.core.dao.MchAppDao;
import com.stx.jeepay.core.entity.MchAppEntity;

import com.stx.jeepay.core.entity.MchPayPassageEntity;
import com.stx.jeepay.core.entity.PayInterfaceConfigEntity;
import com.stx.jeepay.core.entity.PayOrderEntity;
import com.stx.jeepay.core.enums.ApiCodeEnum;
import com.stx.jeepay.core.enums.CS;
import com.stx.jeepay.core.expection.BizException;
import com.stx.jeepay.core.service.MchAppService;
import com.stx.jeepay.core.service.MchPayPassageService;
import com.stx.jeepay.core.service.PayInterfaceConfigService;
import com.stx.jeepay.core.service.PayOrderService;
import com.stx.jeepay.core.utils.StringKit;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


@Service("mchAppService")
public class MchAppServiceImpl extends ServiceImpl<MchAppDao, MchAppEntity> implements MchAppService {

    @Autowired
    private PayOrderService payOrderService;

    @Autowired
    private MchPayPassageService mchPayPassageService;

    @Autowired
    private PayInterfaceConfigService payInterfaceConfigService;

    /**
     * 查询商户应用分页数据
     */
    @Override
    public IPage<MchAppEntity> selectPage(IPage iPage, MchAppEntity mchApp) {
        LambdaQueryWrapper<MchAppEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(mchApp.getMchNo())) {
            queryWrapper.eq(MchAppEntity::getMchNo, mchApp.getMchNo());
        }
        if (StringUtils.isNotEmpty(mchApp.getAppId())) {
            queryWrapper.eq(MchAppEntity::getAppId, mchApp.getAppId());
        }
        if (StringUtils.isNotEmpty(mchApp.getAppName())) {
            queryWrapper.eq(MchAppEntity::getAppName, mchApp.getAppName());
        }
        if (mchApp.getState() != null) {
            queryWrapper.eq(MchAppEntity::getState, mchApp.getState());
        }
        queryWrapper.orderByDesc(MchAppEntity::getCreatedAt);
        IPage<MchAppEntity> pages = this.page(iPage, queryWrapper);
        // 加密秘钥
        pages.getRecords().stream().forEach(item -> item.setAppSecret(StringKit.str2Star(item.getAppSecret(), 6, 6, 6)));
        return pages;
    }

    @Override
    public MchAppEntity selectById(String appId) {
        MchAppEntity mchApp = this.getById(appId);
        if (mchApp==null){
            return null;
        }
        // 对秘钥加密
        mchApp.setAppSecret(StringKit.str2Star(mchApp.getAppSecret(), 6, 6, 6));

        return mchApp;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeByAppId(String appId) {
        // 1.查看当前应用是否存在交易数据
        int payCount = payOrderService.count(new LambdaQueryWrapper<PayOrderEntity>()
                .eq(PayOrderEntity::getAppId, appId));
        if (payCount>0){
            throw new BizException("该应用已存在交易数据，不可删除");
        }

        // 2.删除应用关联的支付通道
        mchPayPassageService.remove(new LambdaQueryWrapper<MchPayPassageEntity>()
                .eq(MchPayPassageEntity::getAppId,appId));

        // 3.删除应用配置的支付参数
        payInterfaceConfigService.remove(new LambdaQueryWrapper<PayInterfaceConfigEntity>()
                .eq(PayInterfaceConfigEntity::getInfoId,appId)
                .eq(PayInterfaceConfigEntity::getInfoType, CS.INFO_TYPE_MCH_APP));

        // 4.删除当前应用
        if (!removeById(appId)){
            throw new BizException(ApiCodeEnum.SYS_OPERATION_FAIL_DELETE);
        }
    }

    @Override
    public MchAppEntity getOneByMch(String mchNo, String appId) {
        return getOne(new LambdaQueryWrapper<MchAppEntity>()
                .eq(MchAppEntity::getMchNo,mchNo)
                .eq(MchAppEntity::getAppId,appId));
    }
}